package com.prs.crm.action.sys;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.XJAction;
import org.courser.ui.PaginationSupport;

import com.prs.crm.context.Constrains;
import com.prs.crm.domain.sys.Form;
import com.prs.crm.domain.sys.FormField;
import com.prs.crm.service.sys.FormService;

public class FormFieldAction extends XJAction{

	private static final long serialVersionUID = 1L;
	
	private FormService formService;
	
	private PaginationSupport pagination;
	
	private Form form;
	
	private FormField field;
	
	private Integer[] fieldIds;
	
	private boolean gridshow;
	
	private boolean  queryfield;

	public void setFormService(FormService formService) {
		this.formService = formService;
	}

	public FormService getFormService() {
		return formService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setForm(Form form) {
		this.form = form;
	}

	public Form getForm() {
		return form;
	}

	public void setField(FormField field) {
		this.field = field;
	}

	public FormField getField() {
		return field;
	}

	public Integer[] getFieldIds() {
		return fieldIds;
	}

	public void setFieldIds(Integer[] fieldIds) {
		this.fieldIds = fieldIds;
	}

	public void setGridshow(boolean gridshow) {
		this.gridshow = gridshow;
	}

	public boolean isGridshow() {
		return gridshow;
	}

	public void setQueryfield(boolean queryfield) {
		this.queryfield = queryfield;
	}

	public boolean isQueryfield() {
		return queryfield;
	}
	
	@Action("listFields")
	public String listFields()
	{
		this.setPagination(this.getFormService().listFormFields(form,getPaginationHelper().prepare(pagination)));
					
		return SUCCESS;
	}
	
	@Action("editField")
	public String editField()
	{	
		
		if (this.getField()!=null)
		{
			if (this.getForm()==null)
			{
				this.setForm(this.getField().getForm());
			}
			if ((this.getField().getFlag().intValue() & Constrains.FORMFIELD_GRIDSHOW) ==Constrains.FORMFIELD_GRIDSHOW)
				this.setGridshow(true);
			else
				this.setGridshow(false);
			if ((this.getField().getFlag().intValue() & Constrains.FORMFIELD_QUERYFIELD) ==Constrains.FORMFIELD_QUERYFIELD)
				this.setQueryfield(true);
			else
				this.setQueryfield(false);
		}
		return SUCCESS;
	}
	
	@Action(value="saveField",results={@Result(name="success",type="redirect",location="listFields?form=${form.id}")})
	public String saveField()
	{
		this.getFormService().saveField(form, field, gridshow, queryfield);
		return SUCCESS;
	}
	
	@Action(value="removeFields",results={@Result(name="success",type="redirect",location="listFields?form=${form.id}")})
	public String removeFields()
	{
		this.getFormService().batchRemove(fieldIds);
		return SUCCESS;
	}

}
